//
//  DestImpressionsFristTableViewCell.m
//  TravelEveryDay
//
//  Created by dlios on 15/10/21.
//  Copyright © 2015年 com.lanou3g. All rights reserved.
//

#import "DestImpressionsFristTableViewCell.h"
#import "BaseHeader.h"
#import "DestImpressionsCollectionViewCell.h"
#import "DestCollectionFlowLayout.h"
#import "DestTraveNotesPhotoesViewController.h"
#import "DestFirstCellInfoViewController.h"

@implementation DestImpressionsFristTableViewCell

- (void)awakeFromNib {
    // Initialization code
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _avatar_sImageView = [[UIImageView alloc]initWithFrame:CGRectMake(5 * WIDTH, 5 * HEIGHT, 36 * HEIGHT  , 36 * HEIGHT)];
        _avatar_sImageView.layer.cornerRadius = 18 *HEIGHT;
        _avatar_sImageView.userInteractionEnabled = YES;
        _button = [UIButton buttonWithType:UIButtonTypeCustom];
        _button.frame = CGRectMake(0, 0, 41 * HEIGHT, 41 * HEIGHT);
        _avatar_sImageView.layer.masksToBounds = YES;
        _nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(36 *HEIGHT +10 * WIDTH, 5 * HEIGHT, 200 * WIDTH, 15 *HEIGHT)];
        _nameLabel.font =[UIFont systemFontOfSize:14.0];
        _twoNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(36 *HEIGHT + 10 * WIDTH,  25 * HEIGHT,200 * WIDTH, 15 *HEIGHT)];
        _twoNameLabel.font =[UIFont systemFontOfSize:14.0f];
        _twoNameLabel.textColor = [UIColor colorWithRed:0.000 green:0.963 blue:1.000 alpha:1.000];
        _trip_textLabel = [[UILabel alloc]initWithFrame:CGRectZero];
        _trip_textLabel.numberOfLines = 0;
        _trip_textLabel.font = [UIFont systemFontOfSize:13.0];
       
        [self.contentView addSubview:_avatar_sImageView];
        [self.contentView addSubview:_nameLabel];
        [self.contentView addSubview:_twoNameLabel];
        [self.contentView addSubview:_trip_textLabel];
        [self.contentView addSubview:_button];
        [self createCollectionview];
    }
    return self;
}
#pragma mark - 写创建collectionView的方法
- (void)createCollectionview {
    _destFlowLayout = [[DestCollectionFlowLayout alloc]init];
    _destFlowLayout.delegate = self;
    
    _destFlowLayout.minimumInteritemSpacing = 5;
    _destFlowLayout.minimumLineSpacing = 5;
    _destFlowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    _destFlowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    _photoesCollectionView  = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:_destFlowLayout];
    _photoesCollectionView.backgroundColor = [UIColor whiteColor];
    _photoesCollectionView.bounces = NO;
    _photoesCollectionView.delegate = self;
    _photoesCollectionView.dataSource =self;
    _photoesCollectionView.showsHorizontalScrollIndicator = NO;
    [_photoesCollectionView registerClass:[DestImpressionsCollectionViewCell class] forCellWithReuseIdentifier:@"destImpressionsCollectionViewCell"];
    [self.contentView addSubview:_photoesCollectionView];
}
#pragma mark - 代理方法  返回各个item大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    NSMutableDictionary *dic  = [_dataModel.waypoints objectAtIndex:indexPath.row];
    NSMutableDictionary *photoDic =[dic objectForKey:@"photo_info"];
    NSNumber *h = [photoDic objectForKey:@"h"];
    NSNumber *w =[photoDic objectForKey:@"w"];
    CGFloat  scale = h.floatValue / w.floatValue;
    CGFloat cellWidth = 150 * WIDTH / scale;
   return CGSizeMake(cellWidth , 150 * HEIGHT);
}
#pragma mark - 重写set方法
- (void)setDataModel:(DestImpressionModel *)dataModel {
    _dataModel = nil;
     [_photoesCollectionView reloadData];
    if ([dataModel.trip_text isEqualToString:@""]) {
     _hiehgt = 0.0;
    }else{
     _hiehgt =[StringWithHeight heightWithText:dataModel.trip_text width:SCREEN_WIDTH - 20 *WIDTH font:[UIFont systemFontOfSize:13.0]];
    }
    _trip_textLabel.frame = CGRectMake(10 *WIDTH, 46 *HEIGHT, SCREEN_WIDTH - 20 * WIDTH, _hiehgt);
    _trip_textLabel.text =[NSString stringWithFormat:@"^_^%@",dataModel.trip_text];
    NSString *urlString = [[dataModel.trip objectForKey:@"user"]objectForKey:@"avatar_m"];
    NSLog(@"%@",[dataModel.trip objectForKey:@"user"]);
    NSURL *url = [NSURL URLWithString:urlString];
    [_avatar_sImageView sd_setImageWithURL:url placeholderImage:PLACEHOLDERIMAGE];
    _nameLabel.text = [[dataModel.trip objectForKey:@"user"]objectForKey:@"name"];
    NSString *twoLabelString = [dataModel.trip objectForKey:@"name"];
    _twoNameLabel.text =[NSString stringWithFormat:@"@ %@",twoLabelString];
    _photoesCollectionView.frame = CGRectMake(10* WIDTH, 46 *HEIGHT + _hiehgt, 355 * WIDTH, 170 *HEIGHT);
    _dataModel = dataModel;
    [_photoesCollectionView reloadData];
    
    
}
#pragma mark - collectionView的代理方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    return  _dataModel.waypoints.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    DestImpressionsCollectionViewCell *imperssionCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"destImpressionsCollectionViewCell" forIndexPath:indexPath];
    
    
    
    NSMutableDictionary *waypointsDic = [_dataModel.waypoints objectAtIndex:indexPath.row];
    NSString *string  = [waypointsDic objectForKey:@"photo"];
    
    
    NSMutableDictionary *dic  = [_dataModel.waypoints objectAtIndex:indexPath.row];
    NSMutableDictionary *photoDic =[dic objectForKey:@"photo_info"];
    NSNumber *h = [photoDic objectForKey:@"h"];
    NSNumber *w =[photoDic objectForKey:@"w"];
    CGFloat  scale = h.floatValue / w.floatValue;
    CGFloat cellWidth = 150 * WIDTH / scale;
    
    [imperssionCell setImageViewImage:string Width:cellWidth];



    NSURL *url = [NSURL URLWithString:string];
    
    [imperssionCell.destImageView sd_setImageWithURL:url placeholderImage:PLACEHOLDERIMAGE];
    imperssionCell.backgroundColor = [UIColor redColor];

    return imperssionCell;
}
#pragma mark - collection的点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog( @"点击第%ld个cell",indexPath.row);
     NSMutableDictionary *dic  = [_dataModel.waypoints objectAtIndex:indexPath.row];

    NSNumber *trip_id = [dic objectForKey:@"trip_id"];
    [self.delegate sendtrip_id:trip_id sendPhoto:[dic objectForKey:@"photo"]];
    DestTraveNotesPhotoesViewController *traveNotes = [[DestTraveNotesPhotoesViewController alloc]init];
    
    for (UIView * next = [self superview]; next; next = next.superview) {
        UIResponder * nextResponder = [next nextResponder];

        if ([nextResponder isKindOfClass: [DestFirstCellInfoViewController class]]) {
            UIViewController *controller = (UIViewController *)nextResponder ;
            traveNotes.trip_id = trip_id;
            traveNotes.pushPhoto =[dic objectForKey:@"photo"];
            [controller.navigationController pushViewController:traveNotes animated:YES];
        }
    }
    
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
